/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.label;

import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JLabel;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.label
 * @date 13/06/2014 - 19:24:59
 */
public class HiperlinkLabel extends JLabel {
	
	private static final long serialVersionUID = 7183403183884211693L;
	
	private final String link;
	
	/**
	 * @param link
	 * @wbp.parser.constructor
	 */
	public HiperlinkLabel(final String link) {
	
		this(link, null);
	}
	
	/**
	 * @param link
	 * @param title
	 */
	public HiperlinkLabel(final String link, final String title) {
	
		super(makeVisibleLink(link, title));
		
		this.link = link;
		
		setName(title);
		setSize(100, 25);
		setHorizontalAlignment(CENTER);
		
		addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(final MouseEvent mouseEvent) {
			
				try {
					//
					Desktop.getDesktop().browse(getURI());
					
				}catch(IOException | URISyntaxException exception) {
					
					exception.printStackTrace();
				}
			}
			
			/*
			 * (non-Javadoc)
			 * 
			 * @see java.awt.event.MouseAdapter#mouseEntered(java.awt.event.MouseEvent)
			 */
			@Override
			public void mouseEntered(final MouseEvent mouseEvent) {
			
				setCursor(new Cursor(Cursor.HAND_CURSOR));
			}
			
			/*
			 * (non-Javadoc)
			 * 
			 * @see java.awt.event.MouseAdapter#mouseExited(java.awt.event.MouseEvent)
			 */
			@Override
			public void mouseExited(final MouseEvent mouseEvent) {
			
				setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			}
		});
	}
	
	/**
	 * @return link
	 */
	public String getLink() {
	
		return link;
	}
	
	/**
	 * @return title
	 */
	public String getTitle() {
	
		return getName();
	}
	
	/**
	 * @return uri
	 * @throws URISyntaxException
	 */
	public URI getURI() throws URISyntaxException {
	
		return new URI(getLink());
	}
	
	/**
	 * @param link
	 * @param title
	 * @return link formated
	 */
	private static String makeVisibleLink(final String link, final String title) {
	
		return "<html><a href=\"" + link + "\">" + (title == null || title.trim().isEmpty() ? link : title) + "</a></html>";
	}
}
